/**
* ...
* @author Default
* @version 0.1
*/

package com.crlclr.ui {
	import caurina.transitions.Tweener;
	
	import com.crl.managers.ResizeableManager;
	import com.crl.ui.controls.Component;
	import com.crlclr.common.LoaderAdapter;
	import com.crlclr.common.SiteStatic;
	import com.crlclr.data.qzone.AlbumVO;
	import com.crlclr.data.qzone.QQAlbumXMLLoader;
	import com.crlclr.events.BookEvent;
	import com.crlclr.managers.*;
	import com.crlclr.ui.itemrenders.AlbumItemRender;
	import com.crlclr.utils.Padding;
	import com.crlclr.utils.StringUtil;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	
	public class AlbumList extends Component {
		private var skinID:String = "SinglePage";
		private var path:String;
		private var globalPath:String = "";
		
		private var padding:Padding;
		private var a:int = 0;
		private var i:int = 0;
		private var bookPath:String;
		private var booksWidth:int;
		
		private var list:Array;
		private var bookList:Array;
		private var errorList:Array;
		
		
		public var selectIndex:int=-1;
		public var isBookState:Boolean = false;
		private var container:Sprite;
		private var willAutoRun:Boolean = false;
		public function AlbumList() {
			padding = new Padding(180, 0);
			bookList = new Array();
			
			errorList=new Array();
		}
		
		override protected  function create():void {
			container = new Sprite();
			addChild(container);
		}	
		
		protected function close(event:Event):void {
			dispatchEvent(new BookEvent(BookEvent.CLOSEMORE));
			
			ResizeableManager.getInstance().remove(this);
		}
		
		public function execute():void{
			var loader:QQAlbumXMLLoader=new QQAlbumXMLLoader();
			loader.addEventListener(Event.COMPLETE,completeHandler);
			
			var qqcodeAlbumPath:String=SiteStatic.qqcodeAlbumsPath;
			LoaderManager.getInstance().addItem(new LoaderAdapter(loader,qqcodeAlbumPath));
			
			ResizeableManager.getInstance().add(this);
		}
		public function autoRun():void {
			willAutoRun = true;
			execute();
		}
		protected function completeHandler(event:Event):void {
			var loader:QQAlbumXMLLoader=event.target as QQAlbumXMLLoader;
			loader.removeEventListener(Event.COMPLETE, completeHandler);
			list = loader.albumList;
			
			bookList.splice(0);
			errorList.splice(0);
			
			i=-1;
			
			loadNext();
		}
		
		private function loadNext():void {
			i++;
			if (i > list.length-1) {
				this.resize(stage.stageWidth, stage.stageHeight);
				
				if (willAutoRun) {
					willAutoRun = false;
					showBook(selectIndex = 0);
				}
				return;
			}
			
			var album:AlbumVO=list[i] as AlbumVO;
			
			var path:String=StringUtil.substitute(album.coverImage);
			var urlRequest:URLRequest=new URLRequest(path);
			
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,itemCompleteHandler);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,itemErrorHandler);
			loader.load(urlRequest);
			
			
		}
		
		private function itemErrorHandler(event:IOErrorEvent):void{
			errorList.push(event.text);
			loadNext();
			trace("itemError")
		}
		
		private function itemCompleteHandler(event:Event):void {
			var target:LoaderInfo=event.target as LoaderInfo;
			target.removeEventListener(Event.COMPLETE,itemCompleteHandler);
			
			var index:int=i;
			
			var s:AlbumItemRender= new AlbumItemRender();
			s.buttonMode=true;
			s.index=index;
			s.y=padding.padY;
			s.data=list[index] as AlbumVO;
			s.x=s.defaultX = index * padding.padX+padding.padX/2;
			
			s.addEventListener(MouseEvent.CLICK,bookClickHander);
			s.bitmap=target.content as Bitmap;
			
			bookList.push(s);
			
			container.addChild(s);
			
			booksWidth = container.width+(padding.padX-s.width);
			loadNext();
		}
		
		private function bookClickHander(event:MouseEvent):void {
			if (willAutoRun) {
				return;
			}
			
			var albumItem:AlbumItemRender=event.currentTarget as AlbumItemRender;
			
			showBook(albumItem.index);
		}
		public function showBook(index:int):void {
			
			this.mouseChildren=false;
			
			isBookState = true;
			selectIndex = index;
			removeEventListener(Event.ENTER_FRAME, enterHander);
			
			var len:int = bookList.length;
			var newX:int=0;
			
			for (var i:int = 0; i < len; i++ ) {
				var sub:int = i -index;
				var book:*= bookList[i];
				if (sub == 0) {
					newX = stage.stageWidth / 2 - book.defaultX;
					Tweener.addTween(container,{x:newX,time:1,onComplete:onShowBook})
					continue;
				}else if (sub > 0) {
					newX =book.defaultX-container.x+stage.stageWidth/2;
				}else {
					newX =book.defaultX+container.x-stage.stageWidth/2;
				}
				Tweener.addTween(book,{x:newX,time:2})
			}
			
		}
		
		private function onShowBook():void {
			var e:BookEvent = new BookEvent(BookEvent.BOOKSELECT);
			e.album =list[selectIndex] as AlbumVO;
			dispatchEvent(e);
		}
		
		
		/**
		 * 重新排位; 
		 * 
		 */		
		public function resetBook():void {
			
			this.mouseChildren=true;
			
			if (selectIndex == -1) return;
			var subx:int = stage.stageWidth - booksWidth;
			if (container.x < subx || container.x > 0)  {
				if (container.x > 0) {
					subx = 0;
				}
				Tweener.addTween(container,{x:subx,time:3,onComplete:onCloseBook})
			}else {
				onCloseBook();
			}
			
			var len:int = bookList.length;
			
			for (var i:int = 0; i < len; i++ ) {
				var book:*= bookList[i];
				Tweener.addTween(book,{x:book.defaultX,time:1})
			}
		}
		private function onCloseBook():void {
			addEventListener(Event.ENTER_FRAME, enterHander);
		}
		
		public function overHander(event:MouseEvent):void {
			
			var target:Sprite = event.target as Sprite;
			Tweener.addTween(target,{scaleX:1.1,scaleY:1.1,time:.1,transition:"easeinsine"})
		}
		public function outHander(event:MouseEvent):void {
			var target:Sprite = event.target as Sprite;
			Tweener.addTween(target, { scaleX:1.0, scaleY:1.0, time:.1,transition:"easeinsine" } )
		}
		
		public override function resize(w:int,h:int):void {
			container.y=h/2;
		}
		private function stageHander(event:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, stageHander);
		}
		
		
		public function enterHander(event:Event):void {
			var w:int = stage.stageWidth;
			
			if (!stage||booksWidth-w<0) return;
			var mx:int = stage.mouseX;
			var my:int = stage.mouseY;
			var h:int = stage.stageHeight;
			
			if(container.hitTestPoint(mx,my)){
				if (mx>w-100) {
					a=-15;	
				} else if (mx<100) {
					a=15;
				} else {
					a=0;
				}
			}else {
				a = 0;
			}
			
			container.x+=a;
			if(container.x>0){
				container.x = 0;
				return;
			}
			var subx:int = w - booksWidth;
			if (container.x < subx) {
				container.x = subx;
			}
			
			
		}
	}
	
}
